package com.all.play.mypsm.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.all.play.mypsm.R;
import com.all.play.mypsm.home.bean.HomeResaultBeanData;
import com.all.play.mypsm.utils.Constants;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/3/31.
 */

public class SeckillVhAdapter extends RecyclerView.Adapter<SeckillVhAdapter.ViewHolder> {

    private Context mContext;
    private List<HomeResaultBeanData.ResultBean.SeckillInfoBean.ListBean> list;

    public SeckillVhAdapter(Context mContext,
                   List<HomeResaultBeanData.ResultBean.SeckillInfoBean.ListBean> list) {
        this.mContext=mContext;
        this.list = list;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_seckill, null);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        HomeResaultBeanData.ResultBean.SeckillInfoBean.ListBean listBean = list.get(position);
        if(listBean!=null){
            Glide.with(mContext).load(Constants.BASE_URl_IMAGE+listBean.getFigure()).into(holder.iv_figure);
            holder.tv_cover_price.setText(listBean.getCover_price());
            holder.tv_origin_price.setText(listBean.getOrigin_price());
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv_figure;
        private TextView tv_cover_price;
        private TextView tv_origin_price;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_figure=itemView.findViewById(R.id.iv_figure);
            tv_cover_price=itemView.findViewById(R.id.tv_cover_price);
            tv_origin_price=itemView.findViewById(R.id.tv_origin_price);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(mContext, ""+getLayoutPosition(), Toast.LENGTH_SHORT).show();
                    if(onseckilllistener!=null){
                        onseckilllistener.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
    }

    private OnSeckillListener onseckilllistener;

    public void setOnseckilllistener(OnSeckillListener onseckilllistener) {
        this.onseckilllistener = onseckilllistener;
    }

    public interface OnSeckillListener{
        void onItemClick(int position);
    }
}
